package com.hiep.connect.client.presenter;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasKeyUpHandlers;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.hiep.connect.client.UserRpcServiceAsync;
import com.hiep.connect.client.event.UserLoginEvent;
import com.hiep.connect.client.event.UserRegisterEvent;
import com.hiep.connect.shared.MyUser;

public class LoginViewPresenter implements Presenter {

	public interface LoginDisplay {
		HasClickHandlers getLoginButton();

		HasClickHandlers getRegisterButton();

		HasKeyUpHandlers getPasswordField();

		HasValue<String> getEmailAddress();

		HasValue<String> getPassword();

		Widget asWidget();
	}

	private final HandlerManager eventBus;
	private final LoginDisplay loginDisplay;
	private final UserRpcServiceAsync userService;

	public LoginViewPresenter(UserRpcServiceAsync userService,
			HandlerManager eventBus, LoginDisplay loginDisplay) {
		this.userService = userService;
		this.eventBus = eventBus;
		this.loginDisplay = loginDisplay;
		bind();
	}

	public void bind() {
		this.loginDisplay.getLoginButton().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				doLogin();
			}
		});

		this.loginDisplay.getPasswordField().addKeyUpHandler(
				new KeyUpHandler() {

					@Override
					public void onKeyUp(KeyUpEvent event) {
						// TODO Auto-generated method stub
						if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
							doLogin();
						}

					}
				});

		this.loginDisplay.getRegisterButton().addClickHandler(
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						doRegister();
					}
				});

	}

	public void go(final HasWidgets container) {
		container.clear();
		container.add(loginDisplay.asWidget());
	}

	private void doRegister() {
		String email = loginDisplay.getEmailAddress().getValue();
		String password = loginDisplay.getPassword().getValue();

		MyUser user = new MyUser(email);
		user.setPassword(password);
		
		eventBus.fireEvent(new UserRegisterEvent(user));
		
//		userService.register(email, password, new AsyncCallback<MyUser>() {
//
//			@Override
//			public void onFailure(Throwable caught) {
//				Window.alert("Failed login attempts");
//			}
//
//			@Override
//			public void onSuccess(MyUser result) {
//				eventBus.fireEvent(new UserRegisterEvent(result));
//			}
//		});
	}

	private void doLogin() {
		String email = loginDisplay.getEmailAddress().getValue();
		String password = loginDisplay.getPassword().getValue();
		userService.login(email, password, new AsyncCallback<MyUser>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Failed login attempts");

			}

			@Override
			public void onSuccess(MyUser result) {
				eventBus.fireEvent(new UserLoginEvent(result));
			}
		});
	}
}
